package library.books_author;

import library.model.BooksAuthorPojo;
import javax.swing.table.AbstractTableModel;

/**
 * @author Dmitry
 */
public class BooksAuthorTableModel extends AbstractTableModel {
    private String[] COLUMNS = new String[]{"Название Книги", "Автор"};
    private final int BOOK_NAME = 0;
    private final int AUTHOR = 1;
    private BooksAuthorPojo[] data = new BooksAuthorPojo[0];

    public BooksAuthorPojo getObject(int row){
        return data[row];
    }
    
    public void setData(BooksAuthorPojo[] data) {
        if(data==null){
            data=new BooksAuthorPojo[0];
        }
        this.data = data;
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMNS[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        BooksAuthorPojo pojo = data[rowIndex];
        switch (columnIndex) {
            case BOOK_NAME:
                return pojo.getBookName();
            case AUTHOR:
                return pojo.getAuthor();
            default:
                throw new RuntimeException("Cannot extract data from column " + columnIndex);
        }
    }
}
